#pragma once

#include <atlmisc.h>
#include "stdafx.h"

class CInstalledPrinters : public CSimpleArray<CString>
{
public:
	CInstalledPrinters(void)
	{
		GetPrinters();
	}

	void GetPrinters(void)
	{		
		DWORD dwSize = 0;
		DWORD dwPrinters = 0;
		// Enumerate all installed printers
		if (!::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL,
			5, NULL, 0, &dwSize, &dwPrinters))
		{
			// Check for ERROR_INSUFFICIENT_BUFFER
			// If something else, then quit
			if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
				return;
			// Fall through
		}
		// Allocate some buffer memory
		LPBYTE pBuffer = new BYTE [dwSize];
		if (pBuffer == NULL)
			return;
		// Fill the buffer
		// Again, this depends on the O/S
		if (!::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL,
			5, pBuffer, dwSize, &dwSize, &dwPrinters))
		{
			// Error - unlikely though
			// as first call to EnumPrinters
			// succeeded!
			return;
		}
		// Do we have any printers?
		if (dwPrinters == 0)
			return;
		// Cast to PRINTER_INFO_2
		PRINTER_INFO_5* pInfo = (PRINTER_INFO_5*)pBuffer;
		// Loop adding the printers to the list
		for (DWORD i = 0; i < dwPrinters; i++, pInfo++)
			Add(CString(pInfo->pPrinterName));
		delete [] pBuffer;
	}
};